package com.binvshe.binvshe.organization;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.organization.entity.OrgItemEntity;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.f2prateek.dart.Nullable;
import com.pachong.android.baseuicomponent.activity.SwipeListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/9/18
 */
public class ClubSearchResultActivity extends SwipeListActivity {
    private static final String KEY_SEARCH_TYPE = "Search_Type";
    private static final String KEY_SEARCH_CONTENT = "Search_content";
    private static final String KEY_TYPEID = "TYPEID";
    private static final String KEY_TITLE = "TITLE";
    public static final int USE_KEY = 952;
    public static final int USE_TYPEID = 724;

    @InjectExtra(KEY_SEARCH_TYPE)
    int mSearchType = 0;
    @Nullable
    @InjectExtra(KEY_SEARCH_CONTENT)
    String mSearchContent;
    @Nullable
    @InjectExtra(KEY_TYPEID)
    int mTypeID=500;
    @Nullable
    @InjectExtra(KEY_TITLE)
    String title = "结果";
    private int mPage = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Dart.inject(this);
        super.onCreate(savedInstanceState);
            initTitle();

    }

    @Override
    protected void onResume() {
        super.onResume();
        onStartLoading();
    }

    private void initTitle() {
        enableBackButton();
        setTitle(title);
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        getAdapter().notifyDataSetChanged();
        onStartLoading();

    }

    /**
     * 通过关键字查找
     *
     * @param context
     * @param type
     * @param content
     */
    public static void start(Context context, int type, String content) {
        Intent starter = new Intent(context, ClubSearchResultActivity.class);
        starter.putExtra(KEY_SEARCH_TYPE, type);
        starter.putExtra(KEY_SEARCH_CONTENT, content);
        context.startActivity(starter);
    }

    /**
     * 通过分类id查找
     *
     * @param context
     * @param type
     * @param typeId
     */
    public static void start(Context context, int type, int typeId) {
        Intent starter = new Intent(context, ClubSearchResultActivity.class);
        starter.putExtra(KEY_SEARCH_TYPE, type);
        starter.putExtra(KEY_TYPEID, typeId);
        context.startActivity(starter);
    }

    /**
     * 通过分类id查找
     *
     * @param context
     * @param type
     * @param typeId
     */
    public static void start(Context context, int type, int typeId, String title) {
        Intent starter = new Intent(context, ClubSearchResultActivity.class);
        starter.putExtra(KEY_SEARCH_TYPE, type);
        starter.putExtra(KEY_TYPEID, typeId);
        starter.putExtra(KEY_TITLE, title);
        context.startActivity(starter);
    }


    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new OrgListAdapter(this);
    }

    @Override
    public void onStartLoading() {
        getOrgList();
    }

    ListDataRequestListener<OrgItemEntity> mRequestListener = new ListDataRequestListener<OrgItemEntity>(this, OrgItemEntity.class);

    /**
     * 获取社团列表
     */
    private void getOrgList() {
        if (mPage == 0 || mRequestListener.getResponseDataParser().hasMore()) {
            Log.d("ClubSearchResultActivit", "mSearchType:" + mSearchType);
            if (mRequestListener.getResponseDataParser().hasMore()) {
                mPage++;
            }
            IHttpRequest request = new MyHttpRequest(this);
            UrlParams params = new UrlParams();
            params.put("page", mPage + "");
            params.put("size", 8 + "");
            switch (mSearchType) {
                case USE_KEY:
                    params.put("keyword", mSearchContent);
                    break;
                case USE_TYPEID:
                    Log.d("kyluzoi", "mTypeID:" + mTypeID);
                    params.put("catid", mTypeID + "");
                    break;
            }
            request.get(UrlCenter.GET_ORG_LIST, params, mRequestListener);
        }
    }
}
